<?php
    session_start();
    include_once '../../../../controlador/php/conexion/Conexion.php'; 
    $con = new Conexion();
    
    $dato = $_REQUEST['dat'];
    $filtro = $_REQUEST['dat2'];
    $tc = $_REQUEST ['tc'];
    $f1 = $_REQUEST['f1'];
    $f2 = $_REQUEST['f2'];
    $doc = $_REQUEST ['doc'];
    $FechaI = $_REQUEST['FechaI'];
    $FechaF = $_REQUEST['FechaF'];
    
    $FechaInicio = date('Y-m-d 00:00:00.000',strtotime($FechaI));
    $FechaFinal = date('Y-m-d 23:59:00.000',strtotime($FechaF));
    
    $fecha1 = date('Y-m-d 00:00:00.000',strtotime($f1));
    $fecha2 = date('Y-m-d 23:59:00.000',strtotime($f2));
    
    if($filtro == "1"){
        $consulta = "where PACNUMDOC ='".$dato."' order by prafech desc";
        $consulta2 = "";
    }else if($filtro == "5"){
        $consulta = "where GDENOMBRE LIKE '%".$dato."%' order by prafech desc";
        $consulta2 = "";
    }else if ($filtro == "2"){
        $consulta = "where GEEDESCRI LIKE '%".$dato."%' order by prafech desc";
        $consulta2 = "";
    }else if ($filtro == "3"){
        $consulta = "where ser_descripcion LIKE '%".$dato."%' order by prafech desc";
        $consulta2 = "";
    }else if ($tc == "1"){
        $consulta = "where PACNUMDOC ='".$doc."' order by prafech desc";    
        $consulta2 = "";
    }else if($tc == "2"){
        $consulta = "where prafech between '".$fecha1."' and '".$fecha2."'";
        $consulta2 = "";
    }else if ($filtro == "4"){
        $consulta = "where proestado = '".$dato."' order by prafech desc";
        $consulta2 = "";
    }else if($filtro == "6"){
        $consulta = "where prafech between '".$FechaInicio."' and '".$FechaFinal."' order by prafech desc";
        $consulta2 = "";
    }
    else{
        $consulta = "outer apply (select top 1 hiscancelado as cancelado 
                                      from pchisambu
                                      where pchisambu.proid = pgambu.proid
                                      order by hisfecha desc)as cancel
                    where prafech > CONVERT(DATETIME, GETDATE() - 30, 102)
                    order by prafech desc";
        $consulta2 = ",cancel.cancelado";
    }
    
    $select = "select pratprogram,A.OID as oidgp,programa.praoid,PACNUMDOC,ltrim(rtrim(PACPRINOM)) + ' ' +ltrim(rtrim(PACSEGNOM)) AS nombres,ltrim(rtrim(PACPRIAPE)) + ' ' +ltrim(rtrim(PACSEGAPE)) AS apellidos,praoid,prafecven,pramedico,serviciosL.ser_oid,serviciosL.ser_descripcion,GENESPECI.GEEDESCRI,GENESPECI.OID,
               pracup1,pracodser1,praproce1,pracuproce1,pracup2,pracodser2,praproce2,pracuproce2,pracup3,pracodser3,praproce3,pracuproce3,
               pracup4,pracodser4,praproce4,pracuproce4,pracup5,pracodser5,praproce5,pracuproce5,pracup6,pracodser6,praproce6,pracuproce6,prafech,
               GDENOMBRE,
                        CAST(DATEDIFF(DD, A.GPAFECNAC, GETDATE()) / 365.25 AS INT) AS EDAD,proestado,praobs,praoid ".$consulta2." 
                from pcprogramaA as programa left outer join serviciosL on programa.praservicio = serviciosL.ser_oid
                 left outer join ".$con->getReal()."..GENESPECI on programa.praespeciali = ".$con->getReal()."..GENESPECI.OID
                 left outer join ".$con->getReal()."..GENPACIEN as A on programa.genPacien = A.OID
                 LEFT OUTER JOIN ".$con->getReal()."..GENDETCON ON A.GENDETCON = GENDETCON.OID
                 left outer join pcprogambu as pgambu on programa.praoid = pgambu. folioid
                       ".$consulta;
    $res = $con->query($select);
    
    while($res2 = mssql_fetch_array($res)){
        $listas[] = array_map(utf8_encode,$res2);
    }
    
    $con->cerrarConexion();
    
    print_r(json_encode($listas));
?>
